//
//  HMProgressImageView.swift
//  SinaWeiBoProject
//
//  Created by 房城鸿 on 15/10/23.
//  Copyright © 2015年 房兰峰. All rights reserved.
//

import UIKit

class HMProgressImageView: UIImageView {

    var progress: CGFloat = 0 {
        didSet{
            progressView.progress = progress
        }
    }
    private lazy var progressView: HMProgressView = {
        let p = HMProgressView()
        p.backgroundColor = UIColor.clearColor()
        //添加控件
        self.addSubview(p)
        //设置大小
        p.frame = self.bounds
        return p
    }()
    
    /**
    在imageView 中，drawRect 函数不会被调用到
    */
//    override func drawRect(rect: CGRect) {
//        printLog("drawRect")
//    }
    
    //类中类，专供 HMProgressImageView 使用
    private class HMProgressView:UIView{
    ///  进度条值 0~1
        var progress:CGFloat = 0{
            didSet{
                setNeedsDisplay()
            }
        }
        private override func drawRect(rect: CGRect) {
            
            if progress >= 1{
                return
            }
            //绘制曲线
            //添加曲线
            /**
            1.中心点
            2.半径
            3。起始角度
            4.结束角度
            5.是否顺时针
            
            self.center 是相对父视图
            */
            let cent = CGPoint(x: rect.width*0.5, y: rect.height*0.5)
            let radius = min(rect.width, rect.height) * 0.5
            let start = -CGFloat(M_PI_2)
            let end = 2 * CGFloat(M_PI) * progress + start
            
            let path = UIBezierPath(arcCenter: cent, radius: radius, startAngle: start, endAngle: end, clockwise: true)
            
            //添加指向圆心的曲线
            path.addLineToPoint(cent)
            path.closePath()
            
            //设置属性 ，在实际应用中，动画只是点缀，不要抢
            UIColor(white: 0.7, alpha: 0.7).setFill()
            path.fill()
        }
    }
}
